import pyb_message
from MyMotor import bluetooth,Tracking,TestHowRun,HowRun,HardStop,RaspiTracking,SlowStop,FrontBackCheckRun1
from pyb import delay


def raspiTracking(BLE):
    global mode
    global TrackSpeed
    print('Tracking')
    while 1:
        if BLE.uart.any():  # 查询是否有信息
            text = BLE.uart.read(1)  # 默认单次最多接收128字节
            if text.find(b'o') > -1:
                HardStop()
                mode = '3'
                return 3
            elif text.find(b'a') > -1:
                HardStop()
                mode = "1"  # 蓝牙
                return 1
        else:
            text = pyb_message.receive_text()
            if text == 'go_ahead':
                lrgt = FrontBackCheckRun1()
                lrg = lrgt[0]  # 取前后循迹模块信号值做出下一步运动的判断
                RunTime = lrgt[1]  # 取前后循迹模块信号值做出下一步运动的判断
                ss = SlowStop()  # 根据左面循迹模块判断是否停下或减速
                print(ss)
                if ss == 's':
                    print('Slow')
                    TrackSpeed = 30
                    HowRun(lrg, RunTime)
                    delay(50)
                elif ss == 'f':
                    print('Fast')
                    TrackSpeed = 60
                    HowRun(lrg, RunTime)
                    delay(50)
                elif ss == 'c':
                    print('Stop')
                    HardStop()
                    pyb_message.send_text('stop')
                    delay(50)
                else:
                    pass